/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductOverview.java
 *
 * Created on 28-nov-2011, 12:22:25
 */
package view;

import java.util.List;
import java.util.Locale.Category;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Product;

/**
 *
 * @author Bryan
 */
public class ProductOverview extends javax.swing.JPanel {

    /** Creates new form ProductOverview */
    public ProductOverview() {
        initComponents();
        WinkelApplication.mainWindow.setTitle("Producten");
        List<Product> products = WinkelApplication.getQueryManager().getAllProducts();
        DefaultTableModel modelProd = (DefaultTableModel) this.tblProducts.getModel();
        
        List<model.Category> categories = WinkelApplication.getQueryManager().getCategories();
        DefaultTableModel modelCat = (DefaultTableModel) this.tblCategories.getModel();
        
        for(Product product: products) {
            modelProd.addRow(new Object[] {new Integer(product.getProductId()),
            product.getCategory(),
            product.getName(),
            product.getPriceFormatted(),
            product.getDescription(),
            product.getStock(),
            product.getMinStock()});
        }
        modelCat.addRow(new Object[] {new Integer(0),
            "Alle categorieën"});
        
        for(model.Category category: categories) {
            modelCat.addRow(new Object[] {new Integer(category.getCategoryId()),
            category.getName()});
        }        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollProducts = new javax.swing.JScrollPane();
        tblProducts = new javax.swing.JTable();
        btnProdNew = new javax.swing.JButton();
        btnProdEdit = new javax.swing.JButton();
        btnProdDelete = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCategories = new javax.swing.JTable();
        btnCatNew = new javax.swing.JButton();
        btnCatEdit = new javax.swing.JButton();
        btnCatDelete = new javax.swing.JButton();
        Vooraad = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(1000, 700));
        setMinimumSize(new java.awt.Dimension(1000, 700));

        tblProducts.setAutoCreateRowSorter(true);
        tblProducts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Product_id", "Categorie", "Naam", "Prijs", "Omschrijving", "Voorraad", "Min Voorraad"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblProducts.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        tblProducts.setName("tblProducts"); // NOI18N
        tblProducts.getTableHeader().setReorderingAllowed(false);
        scrollProducts.setViewportView(tblProducts);
        tblProducts.getColumnModel().getColumn(0).setPreferredWidth(30);
        tblProducts.getColumnModel().getColumn(3).setPreferredWidth(30);
        tblProducts.getColumnModel().getColumn(5).setPreferredWidth(20);
        tblProducts.getColumnModel().getColumn(6).setPreferredWidth(28);

        btnProdNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgAdd.gif"))); // NOI18N
        btnProdNew.setText("Toevoegen");
        btnProdNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProdNewActionPerformed(evt);
            }
        });

        btnProdEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgModify.png"))); // NOI18N
        btnProdEdit.setText("Wijzigen");
        btnProdEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProdEditActionPerformed(evt);
            }
        });

        btnProdDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgDelete.png"))); // NOI18N
        btnProdDelete.setText("Verwijderen");
        btnProdDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProdDeleteActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png"))); // NOI18N
        btnBack.setText("Terug");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        tblCategories.setAutoCreateRowSorter(true);
        tblCategories.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Naam"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCategories.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CategoryClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblCategories);
        tblCategories.getColumnModel().getColumn(0).setPreferredWidth(30);
        tblCategories.getColumnModel().getColumn(0).setMaxWidth(30);

        btnCatNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgAdd.gif"))); // NOI18N
        btnCatNew.setText("Toevoegen");
        btnCatNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCatNewActionPerformed(evt);
            }
        });

        btnCatEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgModify.png"))); // NOI18N
        btnCatEdit.setText("Wijzigen");
        btnCatEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCatEditActionPerformed(evt);
            }
        });

        btnCatDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgDelete.png"))); // NOI18N
        btnCatDelete.setText("Verwijderen");
        btnCatDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCatDeleteActionPerformed(evt);
            }
        });

        Vooraad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/btnAlert.png"))); // NOI18N
        Vooraad.setText("Vooraadtekort");
        Vooraad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VooraadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnCatEdit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCatDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCatNew)))
                .addGap(18, 18, 18)
                .addComponent(btnBack)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Vooraad, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                .addComponent(btnProdEdit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnProdDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnProdNew)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(353, Short.MAX_VALUE)
                    .addComponent(scrollProducts, javax.swing.GroupLayout.PREFERRED_SIZE, 637, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnProdNew)
                    .addComponent(btnProdDelete)
                    .addComponent(btnProdEdit)
                    .addComponent(btnCatNew)
                    .addComponent(btnCatDelete)
                    .addComponent(btnCatEdit)
                    .addComponent(btnBack)
                    .addComponent(Vooraad))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scrollProducts, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
                    .addGap(40, 40, 40)))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
    WinkelApplication.getInstance().showPanel(new MainMenu());
}//GEN-LAST:event_btnBackActionPerformed

private void btnProdDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProdDeleteActionPerformed
    if(tblProducts.getSelectedRow() != -1){
        int selected = Integer.parseInt(tblProducts.getValueAt(tblProducts.getSelectedRow(), 0).toString());
        WinkelApplication.getQueryManager().deleteProduct(selected);
        WinkelApplication.getInstance().showPanel(new ProductOverview(),1000,480);
    } else {
        WinkelApplication.msgbox("U heeft nog geen product geselecteerd dat u wilt verwijderen");
    }    

}//GEN-LAST:event_btnProdDeleteActionPerformed

private void btnProdEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProdEditActionPerformed
    if(tblProducts.getSelectedRow() != -1){
        int selected = Integer.parseInt(tblProducts.getValueAt(tblProducts.getSelectedRow(), 0).toString());
        WinkelApplication.getInstance().showPanel(new AlterProduct(selected),435,380);
    } else {
        WinkelApplication.msgbox("U heeft nog geen product geselecteerd dat u wilt aanpassen");
    }
    //WinkelApplication.getQueryManager().deleteProduct(delete);
    //
}//GEN-LAST:event_btnProdEditActionPerformed

private void btnProdNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProdNewActionPerformed
    WinkelApplication.getInstance().showPanel(new AlterProduct(),435,380);
}//GEN-LAST:event_btnProdNewActionPerformed

private void CategoryClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CategoryClicked
    if(tblCategories.getSelectedRow() != -1){
        //WinkelApplication.msgbox(.toString());

        
        List<Product> products = WinkelApplication.getQueryManager().getAllProducts();
        DefaultTableModel modelProd = (DefaultTableModel) this.tblProducts.getModel();
        while(modelProd.getRowCount()>0){
            modelProd.removeRow(0);
        }
        int selected = Integer.parseInt(tblCategories.getValueAt(tblCategories.getSelectedRow(), 0).toString());
        for(Product product: products) {
            if(selected==product.getCategoryId() || selected == 0){
                modelProd.addRow(new Object[] {new Integer(product.getProductId()),
                product.getCategory(),
                product.getName(),
                product.getPriceFormatted(),
                product.getDescription(),
                product.getStock(),
                product.getMinStock()});
            }
        }
    }
}//GEN-LAST:event_CategoryClicked

private void btnCatNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCatNewActionPerformed
    WinkelApplication.getInstance().showPanel(new AlterCategory(),435,380);
}//GEN-LAST:event_btnCatNewActionPerformed

private void btnCatEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCatEditActionPerformed
    if(tblCategories.getSelectedRow() != -1){
        int selected = Integer.parseInt(tblCategories.getValueAt(tblCategories.getSelectedRow(), 0).toString());
        WinkelApplication.getInstance().showPanel(new AlterCategory(selected),435,380);
    } else {
        WinkelApplication.msgbox("U heeft nog geen categorie geselecteerd dat u wilt aanpassen");
    }
}//GEN-LAST:event_btnCatEditActionPerformed

private void btnCatDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCatDeleteActionPerformed
    if(tblCategories.getSelectedRow() != -1){
        int selected = Integer.parseInt(tblCategories.getValueAt(tblCategories.getSelectedRow(), 0).toString());
        WinkelApplication.getQueryManager().deleteCategory(selected);
        WinkelApplication.getInstance().showPanel(new ProductOverview(),1000,480);
    } else {
        WinkelApplication.msgbox("U heeft nog geen product geselecteerd dat u wilt verwijderen");
    }    
}//GEN-LAST:event_btnCatDeleteActionPerformed

private void VooraadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VooraadActionPerformed
WinkelApplication.getInstance().showPanel(new StockOverview(),1000,480);
}//GEN-LAST:event_VooraadActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Vooraad;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnCatDelete;
    private javax.swing.JButton btnCatEdit;
    private javax.swing.JButton btnCatNew;
    private javax.swing.JButton btnProdDelete;
    private javax.swing.JButton btnProdEdit;
    private javax.swing.JButton btnProdNew;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane scrollProducts;
    private javax.swing.JTable tblCategories;
    private javax.swing.JTable tblProducts;
    // End of variables declaration//GEN-END:variables
}
